package com.example.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.example.train.business.domain.Train_Carriage;
import com.example.train.business.domain.Train_Seat;
import com.example.train.business.domain.Train_SeatExample;
import com.example.train.business.enums.SeatColEnum;
import com.example.train.business.mapper.Train_SeatMapper;
import com.example.train.business.req.Train_SeatQueryReq;
import com.example.train.business.req.Train_SeatSaveReq;
import com.example.train.business.resp.Train_SeatQueryResp;
import com.example.train.common.resp.PageResp;
import com.example.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Train_SeatService {
    private static final Logger LOG = LoggerFactory.getLogger(Train_SeatService.class);

    @Resource
    private Train_SeatMapper train_SeatMapper;

    @Resource
    private Train_CarriageService train_carriageService;

    /**
     * 乘客注册请求的服务类
     * @param req
     */
    public void save(Train_SeatSaveReq req) {
        DateTime now = DateTime.now();
        Train_Seat train_Seat = BeanUtil.copyProperties(req, Train_Seat.class);
        if(ObjectUtil.isNull(train_Seat.getId())){
            //在service中对以下三种数据赋值，因为请求参数不带这三个值，需要服务端自行设置
            train_Seat.setId(SnowUtil.getSnowflakeNextId());
            train_Seat.setCreateTime(now);
            train_Seat.setUpdateTime(now);
            train_SeatMapper.insert(train_Seat);
        }else{
            train_Seat.setUpdateTime(now);
            train_SeatMapper.updateByPrimaryKey(train_Seat);
        }
    }
    public PageResp<Train_SeatQueryResp> queryList(Train_SeatQueryReq req) {
        Train_SeatExample train_SeatExample = new Train_SeatExample();
        train_SeatExample.setOrderByClause("id asc");
        Train_SeatExample.Criteria criteria = train_SeatExample.createCriteria();
        if(ObjectUtil.isNotNull(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("查询页码是 {}", req.getPage());
        LOG.info("每页条数是 {}", req.getSize());
        PageHelper.startPage(req.getPage(), req.getSize());
        List<Train_Seat> train_SeatList = train_SeatMapper.selectByExample(train_SeatExample);

        PageInfo<Train_Seat> pageInfo = new PageInfo<>(train_SeatList);
        LOG.info("总行数是 {}", pageInfo.getTotal());
        LOG.info("总页数是 {}", pageInfo.getPages());

        List<Train_SeatQueryResp> list = BeanUtil.copyToList(train_SeatList, Train_SeatQueryResp.class);

        PageResp<Train_SeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 删除乘客信息的服务实现
     * @param id
     */
    public void delete(Long id) {
        train_SeatMapper.deleteByPrimaryKey(id);
    }

    /**
     * 为每个车厢生成座位信息
     * @param trainCode
     */
    public void genTrainSeat(String trainCode) {
        DateTime now = DateTime.now();

        // 清空当前车次下的所有的座位记录
        Train_SeatExample trainSeatExample = new Train_SeatExample();
        Train_SeatExample.Criteria criteria = trainSeatExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        train_SeatMapper.deleteByExample(trainSeatExample);

        // 查找当前车次下的所有的车厢
        List<Train_Carriage> carriageList = train_carriageService.selectByTrainCode(trainCode);
        LOG.info("当前车次下的车厢数：{}", carriageList.size());

        // 循环生成每个车厢的座位
        for (Train_Carriage trainCarriage : carriageList) {
            // 拿到车厢数据：行数、座位类型(得到列数)
            Integer rowCount = trainCarriage.getRowCount();
            String seatType = trainCarriage.getSeatType();
            int seatIndex = 1;

            // 根据车厢的座位类型，筛选出所有的列，比如车箱类型是一等座，则筛选出columnList={ACDF}
            List<SeatColEnum> colEnumList = SeatColEnum.getColsByType(seatType);
            LOG.info("根据车厢的座位类型，筛选出所有的列：{}", colEnumList);

            // 循环行数
            for (int row = 1; row <= rowCount; row++) {
                // 循环列数
                for (SeatColEnum seatColEnum : colEnumList) {
                    // 构造座位数据并保存数据库
                    Train_Seat trainSeat = new Train_Seat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setCarriageIndex(trainCarriage.getIndex());
                    trainSeat.setRow(StrUtil.fillBefore(String.valueOf(row), '0', 2));
                    trainSeat.setCol(seatColEnum.getCode());
                    trainSeat.setSeatType(seatType);
                    trainSeat.setCarriageSeatIndex(seatIndex++);
                    trainSeat.setCreateTime(now);
                    trainSeat.setUpdateTime(now);
                    train_SeatMapper.insert(trainSeat);
                }
            }
        }
    }
    public List<Train_Seat> selectByTrainCode(String trainCode) {
        Train_SeatExample trainSeatExample = new Train_SeatExample();
        trainSeatExample.setOrderByClause("`id` asc");
        Train_SeatExample.Criteria criteria = trainSeatExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return train_SeatMapper.selectByExample(trainSeatExample);
    }
}
